package com.study.jvm.reference;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * -Xmx20m -XX:+PrintGCDetails -verbose:gc
 */

public class WeakReferenceGc {
    private static final int _4MB = 1024 * 1024 * 4;

    public static void main(String[] args) {
        weakReference();
    }

    private static void weakReference() {
        List<WeakReference<byte[]>> bytes = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            //关联了引用队列，gc后会把软引用放入引用队列中
            WeakReference<byte[]> e = new WeakReference(new byte[_4MB]);
            bytes.add(e);
            System.out.println(bytes);
        }
        System.gc();
        System.out.println("---------------------- ");
        for (WeakReference<byte[]> e : bytes) {
            System.out.println(e.get());
        }
    }
}
